package com.briup.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.briup.cms.bean.Slideshow;
import com.briup.cms.dao.SlideshowDao;
import com.briup.cms.exception.ServiceException;
import com.briup.cms.service.ISlideshowService;
import com.briup.cms.util.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Slf4j
@Service
public class SlideshowServiceImpl implements ISlideshowService {
 @Autowired
 private SlideshowDao slideshowDao;

 @Override
 public List<Slideshow> queryAllEnable() {
  LambdaQueryWrapper<Slideshow> qw = new
          LambdaQueryWrapper<>();
  qw.eq(Slideshow::getStatus, "启用");
  qw.orderByDesc(Slideshow::getUploadTime);
  List<Slideshow> list = slideshowDao.selectList(qw);
  if (list == null || list.size() == 0)
   throw new ServiceException(ResultCode.DATA_NONE);
  return list;
 }

 @Override
 public IPage<Slideshow> query(Integer page, Integer pageSize,
                               String status, String desc) {
  IPage<Slideshow> p = new Page<>(page, pageSize);
  LambdaQueryWrapper<Slideshow> qw = new LambdaQueryWrapper<>
          ();
  qw.eq(StringUtils.hasText(status), Slideshow::getStatus,
                  status)
          .like(StringUtils.hasText(desc),
                  Slideshow::getDescription, desc)
          .orderByDesc(Slideshow::getUploadTime);
  //执行分页查询
  slideshowDao.selectPage(p, qw);
  if (p.getTotal() == 0)
   throw new ServiceException(ResultCode.DATA_NONE);
  return p;
    }
 @Override
 public Slideshow queryById(Integer id) {
  return slideshowDao.selectById(id);
     }
}